using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class InfoWindowManager : MonoBehaviour
{
    private enum MousePosition
    {
        LeftUp,
        RightUp,
        LeftDown,
        RightDown,
        defualt,
    }




    private static InfoWindowManager instance;
    public static InfoWindowManager Instance
    {
        get
        {
            if (instance == null)
            {
                root = new GameObject("Inf oWindow Root");
                DontDestroyOnLoad(root);
                instance = root.AddComponent<InfoWindowManager>();
                //创建canvas
                Canvas canvas = root.AddComponent<Canvas>();
                canvas.renderMode = renderMode;
                canvas.sortingOrder = sortOrder;
                CanvasScaler canvasScaler = root.AddComponent<CanvasScaler>();
                canvasScaler.uiScaleMode = scaleMode;
                canvasScaler.referenceResolution = screenSize;
                //实例化信息窗
                infoWindowGameObject = Instantiate(Resources.Load<GameObject>(prefabPath));
                infoWindowRectTransform = infoWindowGameObject.GetComponent<RectTransform>();
                infoWindowGameObject.SetActive(false);
                infoWindowGameObject.transform.SetParent(root.transform, false);
                InfoWindow infoWindowScript = infoWindowGameObject.GetComponent<InfoWindow>();
                head = infoWindowScript.head;
                body = infoWindowScript.body;
                foot = infoWindowScript.foot;
                icon = infoWindowScript.icon;
            }
            return instance;
        }
    }


    #region Canvas设置
    //屏幕适配比例，如果不对记得改。
    //一般竖屏是1080*1920，横屏是1920*1080。
    private static Vector2 screenSize = new Vector2(1920, 1080);
    //不建议改。
    private static RenderMode renderMode = RenderMode.ScreenSpaceOverlay;
    //同样不建议改
    private static CanvasScaler.ScaleMode scaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
    //sort order,影响多个canvas的渲染顺序，根据需求更改，这里设置999，在最上层渲染。
    private static int sortOrder = 999;
    #endregion

    private static GameObject root;

    private static GameObject infoWindowGameObject;
    private static RectTransform infoWindowRectTransform;
    private static MousePosition beforePosition = MousePosition.defualt;
    private static Vector2 offset = Vector2.zero;
    private static readonly string prefabPath = "Prefab/Info Window";
    private static Image icon;
    private static TextMeshProUGUI head;
    private static TextMeshProUGUI body;
    private static TextMeshProUGUI foot;
    private static bool onDisplay;
    public static InfoSource Current { get; private set; }
    public void Open(InfoSource infoSource)
    {
        if (infoSource == null)
        {
            return;
        }

        Current = infoSource;
        infoWindowGameObject.SetActive(true);
        onDisplay = true;
        /*
         * 写入数据
         */
        if (string.IsNullOrEmpty(infoSource.icon))
        {
            icon.sprite = null;
            icon.gameObject.SetActive(false);
        }
        else
        {
            icon.gameObject.SetActive(true);
            icon.sprite = Resources.Load<Sprite>(infoSource.icon);
        }
        if (string.IsNullOrEmpty(infoSource.head))
        {
            head.text = null;
        }
        else
        {
            head.text = infoSource.head.Replace("\\n", "\n"); ;
        }
        if (string.IsNullOrEmpty(infoSource.body))
        {
            body.text = null;
        }
        else
        {
            body.text = infoSource.body.Replace("\\n", "\n");
        }
        if (string.IsNullOrEmpty(infoSource.foot))
        {
            foot.text = null;
        }
        else
        {
            foot.text = infoSource.foot.Replace("\\n", "\n");
        }
        /*
         * 修改锚点
         */
        MousePosition mousePosition;
        Vector2 mousePos = Mouse.current.position.ReadValue();

        Vector2 ScreenCenter = new Vector2(Screen.width / 2, Screen.height / 2);
        if (mousePos.x < ScreenCenter.x)
        {
            if (mousePos.y < ScreenCenter.y)
            {
                mousePosition = MousePosition.LeftDown;
            }
            else
            {
                mousePosition = MousePosition.LeftUp;
            }
        }
        else
        {
            if (mousePos.y < ScreenCenter.y)
            {
                mousePosition = MousePosition.RightDown;
            }
            else
            {
                mousePosition = MousePosition.RightUp;
            }
        }
        if (mousePosition != beforePosition)
        {
            beforePosition = mousePosition;
            switch (mousePosition)
            {

                case MousePosition.LeftUp:
                    infoWindowRectTransform.anchorMin = Vector2.up;
                    infoWindowRectTransform.anchorMax = Vector2.up;
                    infoWindowRectTransform.pivot = Vector2.up;
                    offset = new Vector2(50, -50);
                    break;
                case MousePosition.RightUp:
                    infoWindowRectTransform.anchorMin = Vector2.one;
                    infoWindowRectTransform.anchorMax = Vector2.one;
                    infoWindowRectTransform.pivot = Vector2.one;
                    offset = new Vector2(-50, -50);
                    break;
                case MousePosition.LeftDown:
                    infoWindowRectTransform.anchorMin = Vector2.zero;
                    infoWindowRectTransform.anchorMax = Vector2.zero;
                    infoWindowRectTransform.pivot = Vector2.zero;
                    offset = new Vector2(50, 50);
                    break;
                case MousePosition.RightDown:
                    infoWindowRectTransform.anchorMin = Vector2.right;
                    infoWindowRectTransform.anchorMax = Vector2.right;
                    infoWindowRectTransform.pivot = Vector2.right;
                    offset = new Vector2(-50, 50);
                    break;
                default:
                    break;
            }
        }
    }
    public void Close()
    {
        infoWindowGameObject.SetActive(false);
        onDisplay = false;
        Current = null;
    }


    private void Update()
    {
        if (onDisplay)
        {
            Vector2 mousePos = Mouse.current.position.ReadValue();
            infoWindowRectTransform.position = (Vector3)(mousePos + offset);
        }
    }
}
